package com.egao.common.component.scheduled;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.core.log.LogScheduled;
import com.egao.common.module.student.service.StudentSignMajorRecordService;
import com.egao.common.module.student.service.StudentSignRecordService;
import com.egao.common.module.system.entity.Year;
import com.egao.common.module.system.service.YearService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lym
 * @date 2024/4/7 15:05
 */
@ApiModel(description = "学生签约率记录定时任务")
@Slf4j
@Component
public class StudentSignRecordScheduled {

    @Autowired
    private YearService yearService;
    @Autowired
    private StudentSignRecordService studentSignRecordService;
    @Autowired
    private StudentSignMajorRecordService studentSignMajorRecordService;

    @Scheduled(cron = "0 00 22 ? * *")
    @ApiOperation("每天18点记录学院就业率")
    public void recordStudentSignRate() {
        // 毕业年份
        List<Year> yearList = yearService.list(new QueryWrapper<Year>().orderByDesc("id"));
        // 学历
        String[] state = {"0", "1", "2", "3", "4"};

        LogScheduled.info("每天18点记录学院就业率");

        for (Year year : yearList) {
            for (String s : state) {
                try {
                    studentSignRecordService.recordStudentSignRate(year.getId(), s, null, null);
                } catch (Exception e) {
                    String message = StrUtil.format("保存{}年{}学历学院就业率失败。错误原因：{}",
                            year.getId(), s, e.getMessage());
                    LogScheduled.error(message);
                }
            }
        }
    }

    @Scheduled(cron = "0 00 22 ? * *")
    @ApiOperation("每天18点记录专业就业率")
    public void recordStudentMajorSignRate() {
        List<Year> yearList = yearService.list(new QueryWrapper<Year>().orderByDesc("id"));
        String[] state = {"0", "1", "2", "3", "4", "5", "6"};

        LogScheduled.info("每天18点记录专业就业率");

        for (Year year : yearList) {
            for (String s : state) {
                try {
                    studentSignMajorRecordService.recordStudentMajorSignRate(year.getId(), s);
                } catch (Exception e) {
                    String message = StrUtil.format("保存{}年{}学历专业就业率失败。错误原因：{}",
                            year.getId(), s, e.getMessage());
                    LogScheduled.error(message);
                }
            }
        }
    }


}
